<?php

namespace app\app\controller;

use app\app\model\ProjectKYPSModel;
use app\app\model\RandModel;
use app\BaseController;
use think\db\exception\DbException;
use think\facade\Db;

class FeasibilityStudyReply extends BaseController
{
    /**
     * 可研批复列表
     * @return void
     * @throws DbException
     */
    public function list()
    {
        $param = $this->request->param();
        $page_size = input('pageSize', '20');

        empty($param['project_id']) && $this->error('请传入project_id');

        $tmp = Db::table('t_project')
            ->where('id', $param['project_id'])
            ->field('link_project_id')
            ->find();

        $project_id = $tmp['link_project_id'];

        $data = Db::table('t_project_kyps')
            ->alias('a')
            ->leftJoin('sys_file_info b', 'a.project_kyps_file = b.file_id')
            ->field('a.*, b.file_path')
            ->where('project_id', $project_id)
            ->paginate($page_size);

        foreach ($data as &$v) {
            $v['project_kyps_time'] = $v['project_kyps_time'] ? date('Y-m-d', strtotime($v['project_kyps_time'])) : null;
            $v['oper_time'] = $v['oper_time'] ? date('Y-m-d', strtotime($v['oper_time'])) : null;
        }

        empty($data) && $this->error('查询为空');

        $this->success('success', $data->toArray());
    }

    /**
     * 可研批复修改
     * @return void
     */
    public function save()
    {
        $id = input('id', '');
        $param = $this->request->param();

        if (empty($id)) {
            $model = new ProjectKYPSModel();
            $model->id = RandModel::randId();
        } else {
            $model = ProjectKYPSModel::where('id', $id);
        }

        $model->save($param);

        $this->success('success');
    }
}